package mdr.demo



import grails.test.mixin.*
import org.junit.*

/**
 * See the API for {@link grails.test.mixin.services.ServiceUnitTestMixin} for usage instructions
 */
class ProductServiceIntegrationTests extends GroovyTestCase {
	
	def productService

    void testPrductSave() {
        def product = new Product(name: "kindle", 
        		category: "electronics", description: "e-ink product from amazon")
        println("printing ${product}")
        assertNull product.save()
    }
	
	void testProductList() {
		assertNotNull productService
		def obj = productService.list()
		assertNotNull obj
	}
	
	void testNewInstance() {
		assertNotNull productService
		def obj = productService.newInstance()
		println("------> ${obj?.name}")
		assertNotNull obj
	}

	void testGetById() {
		assertNotNull productService
		def obj = productService.getById("1")
		assertNotNull obj
		obj = productService.getById("2")
		assertNull obj
		obj = productService.getById(1)
		assertNotNull obj
		obj = productService.getById(2)
		assertNull obj
		println("------> ${obj}")
	}
	
	void testFindByName() {
		assertNotNull productService
		def obj = new Product(name: "kindle", 
        		category: "electronics", description: "e-ink product from amazon")
		obj.save()
		assertNotNull Product.findByName("kindle")
	}

	void testDelete() {
		assertNotNull productService
		def obj = new Product(name: "kindle", 
        		category: "electronics", description: "e-ink product from amazon")
		obj.save()
		assertNotNull Product.findByName("kindle")
		productService.delete(obj)
		assertNull Product.findByName("kindle")
		println("------> ${obj}")
	}

	void testUpdate() {
		def obj = new Product(name: "kindle", 
        		category: "electronics", description: "e-ink product from amazon")
		obj.save()
		assertNotNull Product.findByName("kindle")
		obj.name = "kindle2"
		productService.update(obj)
		assertNull Product.findByName("kindle")
	}

	void testSave() {
		def params = [id:null, operation:"save", mode:"create", action:"formactions", controller:"product", name: "kindle", category: "electronics", description: "e-ink product from amazon"]
		assertNull Product.findByName("kindle")
		productService.save(params)
		assertNotNull Product.findByName("kindle")
	}

}
